package com.czz.manage.controller.teacher;

import com.czz.common.exception.ResultCodeEnum;
import com.czz.common.vo.R;
import com.czz.manage.entity.Chapter;
import com.czz.manage.service.ChapterService;
import com.czz.manage.vo.ChapterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: ChapterTeacherController
 * @Author Czz
 * @Date 2020/1/22
 * @Time 16:23
 **/
@RestController
@RequestMapping(value = "/teacher/chapters")
public class ChapterTeacherController {

    @Autowired
    ChapterService chapterService;

    /**
    * @Description 新增章节
    * @Param [chapter]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/25
    * @Time 10:35
    **/
    @PostMapping(value = "")
    public R saveChapter(Chapter chapter){
        if (ObjectUtils.isEmpty(chapter)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        chapterService.save(chapter);
        return R.ok();
    }

    /**
    * @Description 根据id查询章节
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/25
    * @Time 10:36
    **/
    @GetMapping(value = "{id}")
    public R getChapterById(@PathVariable String id){
        if (StringUtils.isEmpty(id)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        Chapter chapter = chapterService.getById(id);
        return R.ok().data("item", chapter);
    }

    /**
    * @Description 根据id更新章节
    * @Param [id, chapter]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/25
    * @Time 10:36
    **/
    @PutMapping(value = "{id}")
    public R updateChapterById(@PathVariable String id, Chapter chapter){
        if (StringUtils.isEmpty(id) || ObjectUtils.isEmpty(chapter)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        chapter.setChapterId(id);
        chapterService.updateById(chapter);
        return R.ok();
    }

    /**
    * @Description 根据id删除章节
    * @Param [id]
    * @Return com.czz.common.vo.R
    * @Author Czz
    * @Date 2020/1/25
    * @Time 10:36
    **/
    @DeleteMapping(value = "{id}")
    public R deleteChapterById(@PathVariable String id){
        if (StringUtils.isEmpty(id)) {
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        chapterService.deleteChapterById(id);
        return R.ok();
    }

    /**
    * @Description 根据课程id查询嵌套章节课时列表
    * @Param
    * @Return
    * @Author Czz
    * @Date 2020/1/25
    * @Time 15:52
    **/
    @GetMapping(value = "/nested/{courseId}")
    public R getNestedListByCourseId(@PathVariable String courseId){
        if (StringUtils.isEmpty(courseId)){
            return R.setResult(ResultCodeEnum.INPUT_ERROR);
        }
        List<ChapterVo> chapterVoList = chapterService.getNestedListByCourseId(courseId);
        return R.ok().data("items", chapterVoList);
    }
}
